﻿using ImageManager.Common;
using ImageManager.DBAccess;
using ImageManager.MyExcep;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Net;

namespace ImageManager.ImageApi
{
    public class GlobalExceptions : IExceptionFilter
    {
        public GlobalExceptions()
        {
        }
        public void OnException(ExceptionContext context)
        {

            if (context.Exception is MyException)
            {
                return;
            }
            if (!context.ExceptionHandled)
            {
                MyLogger.Err(String.Format("客户端{0}访问{1}时引发了一个异常{2}",
                    context.HttpContext.Connection.RemoteIpAddress, context.HttpContext.Request.GetDisplayUrl(), ExceptionUtil.FormatExceptionInfo(context.Exception)));
                string msg = "内部异常，请查看日志了解具体原因";
                var result = new ResponseData<String>()
                {
                    Data = new String[] { "操作错误" },
                    Message = msg,
                    SystemCode = SystemCodeEnum.Error,
                };
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                context.HttpContext.Response.WriteAsync(Utils.Serialize(result));
                var instance = ServiceLoader.GetService<DbInstance>();
                if (instance != null) instance.Abort();
                if (!RuntimeVariable.IS_DEVELOP)
                {
                    context.ExceptionHandled = true;
                }
            }
        }
    }
    public class MyTokenGlobalExceptions : IExceptionFilter
    {
        public void OnException(ExceptionContext context)
        {
            if (context.Exception is MyAuthorizationException && !context.ExceptionHandled)
            {
                MyLogger.Auth(String.Format("客户端{0}访问{1}时引发了一个异常{2}",
                    context.HttpContext.Connection.RemoteIpAddress, context.HttpContext.Request.GetDisplayUrl(), ExceptionUtil.FormatExceptionInfo(context.Exception)));
                var instance = ServiceLoader.GetService<DbInstance>();
                if (instance != null) instance.Abort();
                var ex = context.Exception as MyAuthorizationException;
                var result = new ResponseData<String>()
                {
                    Data = new String[] { "操作错误" },
                    Message = ex.Message,
                    SystemCode = SystemCodeEnum.TokenError,
                };
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                context.HttpContext.Response.WriteAsync(Utils.Serialize(result));
                context.ExceptionHandled = true;
            }
        }
    }

    public class MyGlobalExceptions : IExceptionFilter
    {
        public MyGlobalExceptions()
        {
        }
        public void OnException(ExceptionContext context)
        {
            if (context.Exception is MyException
                && !(context.Exception is MyAuthorizationException)
                && !context.ExceptionHandled)
            {
                MyLogger.Err(String.Format("客户端{0}访问{1}时引发了一个异常{2}",
                    context.HttpContext.Connection.RemoteIpAddress, context.HttpContext.Request.GetDisplayUrl(), ExceptionUtil.FormatExceptionInfo(context.Exception)));
                var ex = context.Exception as MyException;
                var result = new ResponseData<String>()
                {
                    Data = new String[] { "操作错误" },
                    Message = ex.Message,
                    SystemCode = SystemCodeEnum.Error,
                };
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                context.HttpContext.Response.WriteAsync(Utils.Serialize(result));
                var instance = ServiceLoader.GetService<DbInstance>();
                if (instance != null) instance.Abort();
                context.ExceptionHandled = true;
            }
        }
    }
}
